package jast.util.es;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import org.apache.commons.codec.binary.StringUtils;


import java.util.*;


/**
 * @author Jast
 * @description ES Dubbo查询数据格式转换
 * @date 2020-12-17 15:04
 */
public class ElasticSearchDataConvert {

    /**
     * @param json
     * @param isSensitiveStatus 是否脱敏，true 脱敏
     * @return com.bjga.common.core.page.ESDataInfo
     * @name toESDataInfo
     * @date 2020/12/17 3:07 下午
     * @description ES Dubbo 返回的结果进行格式转换为通用格式
     * @auther Jast
     */
    public static ESDataInfo toESDataInfo(String json, boolean... isSensitiveStatus) {
        if (StrUtil.isBlank(json)) {
            return new ESDataInfo();
        }
        ESDataInfo esDataInfo = new ESDataInfo();
        try {
            JSONObject jsonObject = JSONUtil.parseObj(json);
            Long took = jsonObject.getLong("took");
            JSONObject hits = jsonObject.getJSONObject("hits");
            Integer total = hits.getInt("total");
            if (total == null) {
                //7.x版本，获取数据量
                try {
                    JSONObject totalObject = hits.getJSONObject("total");
                    total = totalObject.getInt("value");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            JSONArray hitsJSONArray = hits.getJSONArray("hits");
            int size = hitsJSONArray.size();
            List<Map<String, Object>> list = new ArrayList<>();
            for (int i = 0; i < size; i++) {
                JSONObject jsonObject1 = hitsJSONArray.getJSONObject(i);
                String id = jsonObject1.getStr("_id");
                JSONObject source = jsonObject1.getJSONObject("_source");
                String index = jsonObject1.getStr("_index");
                String[] sensitiveFiled = new String[]{};
                Map<String, Object> map = new HashMap<>();

                for (String key : source.keySet()) {
                    Object val = source.get(key);
                    map.put(key,val);
                     map.put("es_id!@#$", id);
                }
                list.add(map);
            }
            esDataInfo.setRows(list);
            esDataInfo.setTotal(total);
            esDataInfo.setTook(took);
            esDataInfo.setMsg("查询成功");
        } catch (Exception e) {
            esDataInfo.setMsg("查询数据异常");
            e.printStackTrace();
        }
        return esDataInfo;
    }




    @lombok.Data
    public static class Data {
        /**
         * 字段名
         */
        private String name;
        /**
         * 字段值
         */
        private String value;

        public Data() {
        }

        public Data(String name, String value) {
            this.name = name;
            this.value = value;
        }
    }

    public static <T> List<T> castList(Object obj, Class<T> clazz) {
        List<T> result = new ArrayList<T>();
        if (obj instanceof List<?>) {
            for (Object o : (List<?>) obj) {
                result.add(clazz.cast(o));
            }
            return result;
        }
        return null;
    }


    /**
     * 返回需要执行的脚本内容
     * @param json
     * @param index
     * @param id
     * @return
     */
    public static String getPurRequest(String esIp ,String json,String index,String id){
        return "curl -XPUT http://"+esIp+"/"+index+"/_doc/"+id+" -H 'Content-Type: application/json' -d'"
                +json+"'";
    }
}
